package salaryDao;

import JDBCtils.DBUtiles;
import salaryEntity.emIfoEntity;
import salaryEntity.managerEntity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class employeeDao {
    private static emIfoEntity getIfoDao(ResultSet resultSet)throws SQLException {
    String employeename= resultSet.getString(1);
    String employeeID= resultSet.getString(2);
    String password= resultSet.getString(3);
    String Email=resultSet.getString(4);
    String sex=resultSet.getString(5);
    String phone=resultSet.getString(6);
    String birthDay=resultSet.getString(7);
//  -------------------------------------------------------
    emIfoEntity emIfo=new emIfoEntity();
    emIfo.setEmployeename(employeename);
    emIfo.setEmployeeID(employeeID);
    emIfo.setPassword(password);
    emIfo.setEmail(Email);
    emIfo.setSex(sex);
    emIfo.setPhone(phone);
    emIfo.setBirthDay(birthDay);
    return emIfo;
    }
//  ---------------------------查询结果操作------------------------------------
    public static List<emIfoEntity> getEmIfoDao(int page,int limit){
        List<emIfoEntity> em=new ArrayList<>();
        Connection connection = DBUtiles.getConnect();
        PreparedStatement statement = null;
        try {
            statement = connection.prepareCall("select * from Employee_Information_Form limit ?, ?");
            statement.setInt(1, (page - 1) * limit);
            statement.setInt(2, limit);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next())
            {
                emIfoEntity st=getIfoDao(resultSet);

                em.add(st);
            }
            DBUtiles.close(resultSet, statement, connection);
        } catch (SQLException e) {
            e.printStackTrace();

        }
        return em;
    }
   // ------------------------查询总条数-----------------------------------
    public static int  totalCount(){
        Connection connection=DBUtiles.getConnect();
        String sql="select count(*) from Employee_Information_Form";
        int rows=0;
        try {
            PreparedStatement statement=connection.prepareCall(sql);
            ResultSet resultSet=statement.executeQuery();
            if (resultSet.next()){
                rows=resultSet.getInt(1);
            }
            DBUtiles.close(resultSet,statement,connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rows;
    }
//    ------------------------获得单行查询结果----------------------------------
    public static emIfoEntity getEmployeeById(String employeeID)
    {
        Connection connection = DBUtiles.getConnect();
        String sql = "select * from Employee_Information_Form where employeeID =?" ;
        PreparedStatement statement;
        emIfoEntity employee = null;
        try {
            statement = connection.prepareCall(sql);
            statement.setString(1, employeeID);
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next())
            {
                employee =getIfoDao(resultSet);
            }
            DBUtiles.close(resultSet, statement, connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return employee;
    }
// ---------------------注册时用于验证员工名是否存在--------------------
public static emIfoEntity getEmployeeByName(String employeename)
{
    Connection connection = DBUtiles.getConnect();
    String sql = "select * from Employee_Information_Form where employeename =?" ;
    PreparedStatement statement;
    emIfoEntity employee = null;
    try {
        statement = connection.prepareCall(sql);
        statement.setString(1, employeename);
        ResultSet resultSet = statement.executeQuery();
        if (resultSet.next())
        {
            employee =getIfoDao(resultSet);
        }
        DBUtiles.close(resultSet, statement, connection);
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return employee;
}
    //-------------------根据员工号,密码进行登录查询--------------------
    private static managerEntity getManagerDao(ResultSet resultSet)throws SQLException {
        String name = resultSet.getString(1);
        String password = resultSet.getString(2);
        managerEntity sg = new managerEntity();
        sg.setName(name);
        sg.setPassword(password);
       return sg;
    }
    public static managerEntity checkManagerNamePassword(String name, String password)
    {
        Connection connection = DBUtiles.getConnect();
        String sql = "select * from manager where  name = ? and password=?" ;
        PreparedStatement statement;
        managerEntity manager = null;
        try {
            statement = connection.prepareCall(sql);
            statement.setString(1, name);
            statement.setString(2,password);
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next())
            {
                manager =getManagerDao(resultSet);
            }
            DBUtiles.close(resultSet, statement, connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return manager;
    }


    //    -----------------------------删除操作---------------------------------
    public static int delEmployeeById(String employeeID){
        Connection connection=DBUtiles.getConnect();
        String sql="delete from Employee_Information_Form where employeeID=?";
        int rows=0;
        try {
            PreparedStatement statement= connection.prepareCall(sql);
            statement.setString(1,employeeID);
            rows=statement.executeUpdate();
            DBUtiles.close(null,statement,connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rows;
    }
    //----------------------------更新操作----------------------------------------
    public static int updateEmployeeInformation(emIfoEntity employee){
        Connection connection = DBUtiles.getConnect();
        String sql = "update Employee_Information_Form set employeename= ?, sex= ?, password= ?,Email = ?,phone=?,birthDay=? where employeeID= ?";
        int rows = 0;
        try {
            PreparedStatement statement = connection.prepareCall(sql);
            statement.setString(1,employee.getEmployeename() );
            statement.setString(2, employee.getSex());
            statement.setString(3, employee.getPassword());
            statement.setString(4, employee.getEmail());
            statement.setString(5, employee.getPhone());
            statement.setString(6, employee.getBirthDay());
            statement.setString(7, employee.getEmployeeID());

            rows = statement.executeUpdate();
            DBUtiles.close(null, statement, connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return rows;
    }
 //-------------------------添加数据---------------------------------------
 public static int addDataEmployeeInfo(emIfoEntity employee){
        Connection connection=DBUtiles.getConnect();
     String sql = "insert into Employee_Information_Form(employeename,employeeID,password,Email,sex,phone,birthDay) values (?,?,?,?,?,?,?)";
     int rows=0;
     try {
         PreparedStatement statement = connection.prepareCall(sql);
         statement.setString(1,employee.getEmployeename() );
         statement.setString(2, employee.getEmployeeID());
         statement.setString(3, employee.getPassword());
         statement.setString(4, employee.getEmail());
         statement.setString(5, employee.getSex());
         statement.setString(6, employee.getPhone());
         statement.setString(7, employee.getBirthDay());

         rows= statement.executeUpdate();
         DBUtiles.close(null, statement, connection);
     } catch (SQLException e) {
         e.printStackTrace();
     }

     return rows;
 }
//    ---------------------------------测试方法----------------------------------
//    public static void sc(){
//        List<selectEntity> stu=getStuGrade(int ,int b);
//        for (selectEntity st:stu) {
//            System.out.println(st);
//        }
//        for (int i=0;i<stu.size();i++){
//            System.out.println(stu);
//        }
    }
//}
